﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.Controls.Joints.CUiJointNodeData
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.ModelObjects;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using UiInternalControls.WPF;
using UiInternalControls.WPF.JointJogControls;


namespace DnProcessSimulateCommands.RobotJog.Controls.Joints;

public class CUiJointNodeData : CUiNodeData
{
  private const double LIMITS_EPSILON = 1E-06;
  private CUiDeviceNodeData m_ownerDeviceData;
  private TxKinematicJointSlider m_sliderControl;
  private TxKinematicJointValue m_valueControl;
  private TxKinematicJointPercentage m_percentageControl;
  private TxKinematicJointLimits m_lsLimitControl = new TxKinematicJointLimits();
  private TxKinematicJointLimits m_hsLimitControl = new TxKinematicJointLimits();
  private bool m_linearJoint = true;
  private double m_initialLowerSoftLimit;
  private double m_initialUpperSoftLimit;
  private int m_refreshControlsExecutingCounter;
  private string m_tooltipText;
  private bool? m_hasVariableLimits;

  public CUiJointNodeData(
    CApRJRobotJogJointsLogic jointJogApplication,
    TxJoint joint,
    CUiDeviceNodeData ownerDeviceData,
    ITxDevice containerDevice,
    bool externalJoint)
    : base(jointJogApplication, (ITxObject) joint, (ImageSource) null, joint.Name)
  {
    this.m_ownerDeviceData = ownerDeviceData;
    this.IsExternalJoint = externalJoint;
    this.m_linearJoint = this.Joint.Type == 2;
    TxJointIconParamsEx jointIconParamsEx = new TxJointIconParamsEx();
    ((TxObjectIconParamsEx) jointIconParamsEx).Obj = (ITxObject) joint;
    jointIconParamsEx.Owner = containerDevice;
    ((TxObjectIconParamsEx) jointIconParamsEx).WithOverlay = true;
    this.m_nodeIcon = TxIconManager.the().GetObjectImageSource((TxObjectIconParamsEx) jointIconParamsEx);
    this.m_sliderControl = new TxKinematicJointSlider(this.Joint);
    this.m_valueControl = new TxKinematicJointValue(this.Joint);
    this.m_percentageControl = new TxKinematicJointPercentage(this.Joint);
    this.m_initialLowerSoftLimit = this.Joint.LowerSoftLimit;
    this.m_initialUpperSoftLimit = this.Joint.UpperSoftLimit;
    this.m_tooltipText = string.Empty;
    if (this.Joint.IsDependent)
    {
      try
      {
        this.m_tooltipText = this.Joint.KinematicsFunction;
      }
      catch (TxGetKinematicsFunctionFailedException ex)
      {
      }
    }
    else if (this.Joint.IsPositionCoupled)
    {
      bool flag = false;
      try
      {
        ITxDevice itxDevice = (ITxDevice) null;
        TxJoint txJoint = (TxJoint) null;
        double num = 0.0;
        this.Joint.GetPositionCouplingAndContext(ref itxDevice, ref txJoint, ref num);
        if (TxJoint.op_Inequality(txJoint, (ITxObject) null))
          this.m_tooltipText = string.Format(RJRobotJogResources.COUPLED_JOINT_TOOLTIP, (object) $"{((ITxObject) txJoint.Device).Name}:{txJoint.Name}", (object) num);
        else
          flag = true;
      }
      catch (Exception ex)
      {
        flag = true;
      }
      if (flag)
        this.m_tooltipText = RJRobotJogResources.INVALID_COUPLING_TOOLTIP;
    }
    else if (TxJointEx.IsFollowing(this.Joint))
    {
      this.m_tooltipText = RJRobotJogResources.FOLLOWING_JOINT_TOOLTIP_BEGINNING;
      foreach (TxJointFollowJointEx leadingJoint in TxJointEx.GetLeadingJoints(this.Joint))
        this.m_tooltipText += string.Format("\n" + RJRobotJogResources.FOLLOWING_JOINT_TOOLTIP_BASE, (object) leadingJoint.FollowJoint.Name, (object) leadingJoint.Factor);
    }
    this.InitControls();
    this.RegisterForEvents();
  }

  private void RegisterForEvents()
  {
    this.m_sliderControl.ValueChanged += new TxWheel.TxWheel_ValueChangedEventHandler(this.SliderControl_ValueChanged);
    this.m_lsLimitControl.TextChanged += new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_hsLimitControl.TextChanged += new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_lsLimitControl.LostFocus += new RoutedEventHandler(this.LimitControl_LostFocus);
    this.m_hsLimitControl.LostFocus += new RoutedEventHandler(this.LimitControl_LostFocus);
    this.m_lsLimitControl.KeyDown += new KeyEventHandler(this.LimitControl_KeyPress);
    this.m_hsLimitControl.KeyDown += new KeyEventHandler(this.LimitControl_KeyPress);
    this.m_lsLimitControl.GotFocus += new RoutedEventHandler(this.LimitControl_Enter);
    this.m_hsLimitControl.GotFocus += new RoutedEventHandler(this.LimitControl_Enter);
    this.m_valueControl.ValueChanged += new RoutedEventHandler(this.ValueControl_ValueChanged);
    this.Joint.LowerSoftLimitSet += new TxJoint_LowerSoftLimitSetEventHandler(this.device_JointLowerSoftLimitSet);
    this.Joint.UpperSoftLimitSet += new TxJoint_UpperSoftLimitSetEventHandler(this.device_JointUpperSoftLimitSet);
    this.m_jointJogApplication.OptionsChange += new CApRJRobotJogJointsLogic.CApRJRobotJogOptionsChangeHandler(this.CApRJRobotJogJointsApp_OptionsChange);
    TxApplication.Options.Motion.LimitsCheckChanged += new TxMotionOptions_LimitsCheckChangedEventHandler(this.Motion_LimitsCheckChanged);
    TxApplication.Options.Motion.IndicateJointWorkingLimitsCheckChanged += new TxMotionOptions_IndicateJointWorkingLimitsCheckChangedEventHandler(this.Motion_IndicateJointWorkingLimitsCheckChanged);
    TxApplication.Options.Motion.JointWorkingLimitsChanged += new TxMotionOptions_JointWorkingLimitsChangedEventHandler(this.Motion_JointWorkingLimitsChanged);
    TxApplication.Options.Color.KinematicsColorChanged += new TxColorOptions_KinematicsColorChangedEventHandler(this.Color_KinematicsColorChanged);
  }

  private void UnregisterForEvents()
  {
    this.m_sliderControl.ValueChanged -= new TxWheel.TxWheel_ValueChangedEventHandler(this.SliderControl_ValueChanged);
    this.m_lsLimitControl.TextChanged -= new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_hsLimitControl.TextChanged -= new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_lsLimitControl.LostFocus -= new RoutedEventHandler(this.LimitControl_LostFocus);
    this.m_hsLimitControl.LostFocus -= new RoutedEventHandler(this.LimitControl_LostFocus);
    this.m_lsLimitControl.KeyDown -= new KeyEventHandler(this.LimitControl_KeyPress);
    this.m_hsLimitControl.KeyDown -= new KeyEventHandler(this.LimitControl_KeyPress);
    this.m_lsLimitControl.GotFocus -= new RoutedEventHandler(this.LimitControl_Enter);
    this.m_hsLimitControl.GotFocus -= new RoutedEventHandler(this.LimitControl_Enter);
    this.m_valueControl.ValueChanged -= new RoutedEventHandler(this.ValueControl_ValueChanged);
    this.Joint.LowerSoftLimitSet -= new TxJoint_LowerSoftLimitSetEventHandler(this.device_JointLowerSoftLimitSet);
    this.Joint.UpperSoftLimitSet -= new TxJoint_UpperSoftLimitSetEventHandler(this.device_JointUpperSoftLimitSet);
    this.m_jointJogApplication.OptionsChange -= new CApRJRobotJogJointsLogic.CApRJRobotJogOptionsChangeHandler(this.CApRJRobotJogJointsApp_OptionsChange);
    TxApplication.Options.Motion.LimitsCheckChanged -= new TxMotionOptions_LimitsCheckChangedEventHandler(this.Motion_LimitsCheckChanged);
    TxApplication.Options.Motion.IndicateJointWorkingLimitsCheckChanged -= new TxMotionOptions_IndicateJointWorkingLimitsCheckChangedEventHandler(this.Motion_IndicateJointWorkingLimitsCheckChanged);
    TxApplication.Options.Motion.JointWorkingLimitsChanged -= new TxMotionOptions_JointWorkingLimitsChangedEventHandler(this.Motion_JointWorkingLimitsChanged);
    TxApplication.Options.Color.KinematicsColorChanged -= new TxColorOptions_KinematicsColorChangedEventHandler(this.Color_KinematicsColorChanged);
  }

  public TxJoint Joint => this.NodeObject as TxJoint;

  public TxKinematicJointSlider SliderControl => this.m_sliderControl;

  public TxKinematicJointValue ValueControl => this.m_valueControl;

  public TxKinematicJointPercentage PercentageControl => this.m_percentageControl;

  public TxKinematicJointLimits LowSoftLimitControl => this.m_lsLimitControl;

  public TxKinematicJointLimits UpperSoftLimitControl => this.m_hsLimitControl;

  public double JointValue
  {
    get => this.GetDisplayValue(this.Joint.CurrentValue);
    set
    {
      double roundedJointValue = CApRJRobotJogJointsLogic.GetRoundedJointValue(value, this.IsLinear());
      this.SliderControl.Value = roundedJointValue;
      this.ValueControl.Value = roundedJointValue;
      this.PercentageControl.Value = roundedJointValue;
    }
  }

  public string LowerSoftLimit => this.GetFormattedValue(this.Joint.LowerSoftLimit);

  public string UpperSoftLimit => this.GetFormattedValue(this.Joint.UpperSoftLimit);

  public string LowerHardLimit => this.GetFormattedValue(this.Joint.HardLimitsData.LowerHardLimit);

  public string UpperHardLimit => this.GetFormattedValue(this.Joint.HardLimitsData.UpperHardLimit);

  public string TooltipText => this.m_tooltipText;

  public bool IsValidLowerSoftLimit(string softLimitText)
  {
    bool flag = true;
    double result;
    if (double.TryParse(softLimitText, out result))
    {
      double realValue = this.GetRealValue(result);
      double jointMinValue = this.GetJointMinValue();
      double jointMaxValue = this.GetJointMaxValue();
      if (realValue < jointMinValue || realValue > jointMaxValue)
        flag = false;
    }
    else
      flag = this.Joint.HardLimitsData.HardLimitsType == null && softLimitText == RJRobotJogResources.INFINITE_LIMIT;
    return flag;
  }

  public bool IsValidSoftLimit(string softLimitText)
  {
    double result;
    if (double.TryParse(softLimitText, out result))
    {
      double realValue = this.GetRealValue(result);
      double jointMinValue = this.GetJointMinValue();
      double jointMaxValue = this.GetJointMaxValue();
      return realValue >= jointMinValue && realValue <= jointMaxValue;
    }
    return this.Joint.HardLimitsData.HardLimitsType == null && softLimitText == RJRobotJogResources.INFINITE_LIMIT;
  }

  public void ResetToLowerHardLimits()
  {
    this.m_lsLimitControl.TextChanged -= new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_lsLimitControl.SetDefaultValueAppearance();
    if (!this.IsVariantJoint())
    {
      this.Joint.LowerSoftLimit = this.Joint.HardLimitsData.LowerHardLimit;
      this.InitControls();
    }
    this.m_lsLimitControl.TextChanged += new TextChangedEventHandler(this.LimitControl_TextChanged);
  }

  public void ResetToUpperHardLimits()
  {
    this.m_hsLimitControl.TextChanged -= new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_hsLimitControl.SetDefaultValueAppearance();
    if (!this.IsVariantJoint())
    {
      this.Joint.UpperSoftLimit = this.Joint.HardLimitsData.UpperHardLimit;
      this.InitControls();
    }
    this.m_hsLimitControl.TextChanged += new TextChangedEventHandler(this.LimitControl_TextChanged);
  }

  public void ResetToHardLimits()
  {
    this.m_lsLimitControl.TextChanged -= new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_hsLimitControl.TextChanged -= new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_lsLimitControl.SetDefaultValueAppearance();
    this.m_hsLimitControl.SetDefaultValueAppearance();
    if (!this.IsVariantJoint())
    {
      this.Joint.LowerSoftLimit = this.Joint.HardLimitsData.LowerHardLimit;
      this.Joint.UpperSoftLimit = this.Joint.HardLimitsData.UpperHardLimit;
      this.InitControls();
    }
    this.m_lsLimitControl.TextChanged += new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_hsLimitControl.TextChanged += new TextChangedEventHandler(this.LimitControl_TextChanged);
  }

  public void ResetToInitialLimitsValues()
  {
    this.m_lsLimitControl.TextChanged -= new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_hsLimitControl.TextChanged -= new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_lsLimitControl.SetDefaultValueAppearance();
    this.m_hsLimitControl.SetDefaultValueAppearance();
    if (!this.IsVariantJoint())
    {
      this.Joint.LowerSoftLimit = this.m_initialLowerSoftLimit;
      this.Joint.UpperSoftLimit = this.m_initialUpperSoftLimit;
      this.InitControls();
    }
    this.m_lsLimitControl.TextChanged += new TextChangedEventHandler(this.LimitControl_TextChanged);
    this.m_hsLimitControl.TextChanged += new TextChangedEventHandler(this.LimitControl_TextChanged);
  }

  public void StartRefreshControls()
  {
    if (this.m_refreshControlsExecutingCounter == 0)
    {
      ++this.m_refreshControlsExecutingCounter;
      this.InitControls();
    }
    else
      ++this.m_refreshControlsExecutingCounter;
  }

  public void EndRefreshControls()
  {
    if (this.m_refreshControlsExecutingCounter <= 0)
      return;
    --this.m_refreshControlsExecutingCounter;
    if (this.m_refreshControlsExecutingCounter != 0)
      return;
    this.SliderControl.UpdateControlColors();
    this.ValueControl.UpdateControlColors();
    this.PercentageControl.UpdateControlColors();
  }

  public bool IsRefreshingControls() => this.m_refreshControlsExecutingCounter != 0;

  private void InitControls()
  {
    this.InitLimitControls();
    this.InitSliderControl();
    this.InitValueControl();
    this.InitPercentageControl();
  }

  private void InitLimitsStatus()
  {
    double lowerSoftLimit = this.Joint.LowerSoftLimit;
    this.m_sliderControl.LeftLimit = !CApRJRobotJogJointsLogic.IsNoLimitValue(lowerSoftLimit, this.IsAngular()) ? (!CApRJRobotJogJointsLogic.AreEqualValues(lowerSoftLimit, this.Joint.HardLimitsData.LowerHardLimit) ? (TxKinematicJointSlider.LimitType) 1 : (TxKinematicJointSlider.LimitType) 2) : (TxKinematicJointSlider.LimitType) 0;
    double upperSoftLimit = this.Joint.UpperSoftLimit;
    if (CApRJRobotJogJointsLogic.IsNoLimitValue(upperSoftLimit, this.IsAngular()))
      this.m_sliderControl.RightLimit = (TxKinematicJointSlider.LimitType) 0;
    else if (CApRJRobotJogJointsLogic.AreEqualValues(upperSoftLimit, this.Joint.HardLimitsData.UpperHardLimit))
      this.m_sliderControl.RightLimit = (TxKinematicJointSlider.LimitType) 2;
    else
      this.m_sliderControl.RightLimit = (TxKinematicJointSlider.LimitType) 1;
  }

  private void InitSliderControl()
  {
    bool limitsCheck = TxApplication.Options.Motion.LimitsCheck;
    this.SliderControl.IsLinear = this.IsLinear();
    this.SliderControl.SetSoftLimits(CApRJRobotJogJointsLogic.GetRoundedJointValue(this.Joint.LowerSoftLimit, this.IsLinear()), CApRJRobotJogJointsLogic.GetRoundedJointValue(this.Joint.UpperSoftLimit, this.IsLinear()), limitsCheck);
    if (limitsCheck)
    {
      this.InitLimitsStatus();
    }
    else
    {
      this.SliderControl.LeftLimit = (TxKinematicJointSlider.LimitType) 0;
      this.SliderControl.RightLimit = (TxKinematicJointSlider.LimitType) 0;
    }
    ((UIElement) this.SliderControl).IsEnabled = !this.Joint.IsDependent;
    if (this.IsRefreshingControls())
      return;
    this.SliderControl.UpdateControlColors();
  }

  private void InitValueControl()
  {
    if (this.IsLinear())
      this.m_valueControl.ValueType = (TxNumericEditValueType) 0;
    else if (this.IsAngular())
      this.m_valueControl.ValueType = (TxNumericEditValueType) 1;
    if (TxApplication.Options.Motion.LimitsCheck)
    {
      this.m_valueControl.LowerBound = CApRJRobotJogJointsLogic.GetRoundedJointValue(this.Joint.LowerSoftLimit, this.IsLinear());
      this.m_valueControl.UpperBound = CApRJRobotJogJointsLogic.GetRoundedJointValue(this.Joint.UpperSoftLimit, this.IsLinear());
    }
    else if (this.IsAngular())
    {
      this.ValueControl.LowerBound = -17453.27507;
      this.ValueControl.UpperBound = 17453.27507;
    }
    else
    {
      this.ValueControl.LowerBound = -999999.0;
      this.ValueControl.UpperBound = 999999.0;
    }
    ((UIElement) this.m_valueControl).IsEnabled = !this.Joint.IsDependent;
    this.m_valueControl.UpdateControlColors();
  }

  private void InitPercentageControl()
  {
    this.m_percentageControl.IsLinear = this.IsLinear();
    if (TxApplication.Options.Motion.LimitsCheck)
    {
      this.m_percentageControl.LowerBound = CApRJRobotJogJointsLogic.GetRoundedJointValue(this.Joint.LowerSoftLimit, this.IsLinear());
      this.m_percentageControl.UpperBound = CApRJRobotJogJointsLogic.GetRoundedJointValue(this.Joint.UpperSoftLimit, this.IsLinear());
    }
    else if (this.m_percentageControl.IsLinear)
    {
      this.m_percentageControl.LowerBound = -999999.0;
      this.m_percentageControl.UpperBound = 999999.0;
    }
    else
    {
      this.m_percentageControl.LowerBound = -17453.27507;
      this.m_percentageControl.UpperBound = 17453.27507;
    }
    this.m_percentageControl.UpdateControlColors();
  }

  private void InitLimitControls()
  {
    ((FrameworkElement) this.m_lsLimitControl).Tag = (object) false;
    ((FrameworkElement) this.m_hsLimitControl).Tag = (object) true;
    if (TxApplication.Options.Motion.LimitsCheck)
    {
      ((UIElement) this.m_lsLimitControl).IsEnabled = !this.IsVariantJoint() && !this.Joint.IsDependent;
      ((UIElement) this.m_hsLimitControl).IsEnabled = !this.IsVariantJoint() && !this.Joint.IsDependent;
      if (this.m_lsLimitControl.Text != this.LowerSoftLimit)
        this.m_lsLimitControl.Text = this.LowerSoftLimit;
      if (this.m_hsLimitControl.Text != this.UpperSoftLimit)
        this.m_hsLimitControl.Text = this.UpperSoftLimit;
    }
    else
    {
      this.m_lsLimitControl.Text = RJRobotJogResources.LIMITS_OFF_TEXT;
      this.m_hsLimitControl.Text = RJRobotJogResources.LIMITS_OFF_TEXT;
      ((UIElement) this.m_lsLimitControl).IsEnabled = false;
      ((UIElement) this.m_hsLimitControl).IsEnabled = false;
    }
    this.SetSoftLomitExceedingHardLimit(this.m_lsLimitControl);
    this.SetSoftLomitExceedingHardLimit(this.m_hsLimitControl);
  }

  private double GetDisplayValue(double realValue)
  {
    double roundedJointValue = CApRJRobotJogJointsLogic.GetRoundedJointValue(realValue, this.IsLinear());
    return !this.IsLinear() ? Math.Round(roundedJointValue / TxApplication.Options.Units.AngularMultiplier, TxApplication.Options.Units.AngularDigitsAfterDecimalPoint) : Math.Round(roundedJointValue / TxApplication.Options.Units.LinearMultiplier, TxApplication.Options.Units.LinearDigitsAfterDecimalPoint);
  }

  private bool IsLinear() => this.m_linearJoint;

  private bool IsAngular() => !this.m_linearJoint;

  public bool IsExternalJoint { get; set; }

  public bool IsVariantJoint() => this.hasVariableHardLimits(this.Joint);

  public bool HasVariableLimits()
  {
    if (!this.m_hasVariableLimits.HasValue)
      this.m_hasVariableLimits = new bool?(this.hasVariableHardLimits(this.Joint) || this.isLeadingForVariantJoint(this.Joint));
    return this.m_hasVariableLimits.Value;
  }

  private bool hasVariableHardLimits(TxJoint joint) => joint.HardLimitsData.HardLimitsType == 2;

  private TxJoint GetDependsOnJointForVariableHardLimits(TxJoint currJoint)
  {
    TxJoint variableHardLimits = (TxJoint) null;
    if ((currJoint.Device as ITxComponent).IsOpenForModeling)
    {
      if (currJoint.HardLimits is TxJointVariableHardLimits hardLimits)
        variableHardLimits = hardLimits.DependsOn;
    }
    else
    {
      TxJointVariableLimitsData variableHardLimitsData = currJoint.HardLimitsData.VariableHardLimitsData;
      if (variableHardLimitsData != null)
        variableHardLimits = variableHardLimitsData.DependsOn;
    }
    return variableHardLimits;
  }

  private bool isLeadingForVariantJoint(TxJoint joint)
  {
    foreach (TxJoint txJoint in (Collection<ITxObject>) joint.Device.AllJointsAfterCompilation)
    {
      if (this.hasVariableHardLimits(txJoint))
      {
        TxJoint variableHardLimits = this.GetDependsOnJointForVariableHardLimits(txJoint);
        if (TxJoint.op_Inequality(variableHardLimits, (ITxObject) null) && TxJoint.op_Equality(variableHardLimits, (ITxObject) joint))
          return true;
      }
    }
    return false;
  }

  public double GetRealValue(double displayValue)
  {
    double realValue = displayValue;
    if (this.IsLinear())
      realValue *= TxApplication.Options.Units.LinearMultiplier;
    else if (this.IsAngular())
      realValue *= TxApplication.Options.Units.AngularMultiplier;
    return realValue;
  }

  public bool IsJointValueOutOfLimits()
  {
    bool flag = false;
    if (CApRJRobotJogJointsLogic.CompareDoubleValues(this.Joint.CurrentValue, this.Joint.LowerSoftLimit) == -1)
      flag = true;
    else if (CApRJRobotJogJointsLogic.CompareDoubleValues(this.Joint.CurrentValue, this.Joint.UpperSoftLimit) == 1)
      flag = true;
    return flag;
  }

  private double GetJointMaxValue() => this.IsLinear() ? 999999.0 : 17453.27507;

  private double GetJointMinValue() => -this.GetJointMaxValue();

  public void TrimJointValueInLimits()
  {
    if (CApRJRobotJogJointsLogic.CompareDoubleValues(this.Joint.CurrentValue, this.Joint.LowerSoftLimit) == -1)
    {
      this.m_jointJogApplication.FireBeforeJointValueChange(this.Joint);
      this.Joint.CurrentValue = this.Joint.LowerSoftLimit;
      this.m_jointJogApplication.FireAfterJointValueChange(this.Joint);
    }
    else
    {
      if (CApRJRobotJogJointsLogic.CompareDoubleValues(this.Joint.CurrentValue, this.Joint.UpperSoftLimit) != 1)
        return;
      this.m_jointJogApplication.FireBeforeJointValueChange(this.Joint);
      this.Joint.CurrentValue = this.Joint.UpperSoftLimit;
      this.m_jointJogApplication.FireAfterJointValueChange(this.Joint);
    }
  }

  public void RefreshControls()
  {
    this.StartRefreshControls();
    this.JointValue = this.Joint.CurrentValue;
    this.EndRefreshControls();
  }

  private void UpdateLimitControl(object limitControl)
  {
    TxKinematicJointLimits kinematicJointLimits = limitControl as TxKinematicJointLimits;
    if (this.IsValidSoftLimit(kinematicJointLimits.Text))
    {
      double result;
      if (double.TryParse(kinematicJointLimits.Text, out result))
      {
        if ((bool) ((FrameworkElement) kinematicJointLimits).Tag)
          this.Joint.UpperSoftLimit = this.GetRealValue(result);
        else
          this.Joint.LowerSoftLimit = this.GetRealValue(result);
        TxApplication.RefreshDisplay();
      }
    }
    else
      this.InitLimitControls();
    this.InitSliderControl();
    this.InitValueControl();
    this.InitPercentageControl();
    kinematicJointLimits.SetDefaultValueAppearance();
  }

  private void SetSoftLomitExceedingHardLimit(TxKinematicJointLimits textBoxControl)
  {
    string exceedsHardLimit = RJRobotJogResources.SOFT_LIMIT_EXCEEDS_HARD_LIMIT;
    double displayValue1;
    bool flag;
    if (Convert.ToBoolean(((FrameworkElement) textBoxControl).Tag))
    {
      double displayValue2 = this.GetDisplayValue(this.Joint.UpperSoftLimit);
      displayValue1 = this.GetDisplayValue(this.Joint.HardLimitsData.UpperHardLimit);
      flag = CUiJointNodeData.CompareLimits(displayValue2, displayValue1, 1E-06) == 1;
    }
    else
    {
      double displayValue3 = this.GetDisplayValue(this.Joint.LowerSoftLimit);
      displayValue1 = this.GetDisplayValue(this.Joint.HardLimitsData.LowerHardLimit);
      flag = CUiJointNodeData.CompareLimits(displayValue3, displayValue1, 1E-06) == -1;
    }
    string str = string.Format(exceedsHardLimit, (object) displayValue1);
    textBoxControl.SetSoftLimitExceedingHardLimit(flag, str);
  }

  public static int CompareLimits(double first, double second, double epsilon)
  {
    if (Math.Abs(first - second) <= epsilon)
      return 0;
    return first > second ? 1 : -1;
  }

  private string GetFormattingString()
  {
    string formattingString = "F";
    if (this.IsLinear())
      formattingString += TxApplication.Options.Units.LinearDigitsAfterDecimalPoint.ToString();
    else if (this.IsAngular())
      formattingString += TxApplication.Options.Units.AngularDigitsAfterDecimalPoint.ToString();
    return formattingString;
  }

  private string GetFormattedValue(double value)
  {
    return !CApRJRobotJogJointsLogic.IsNoLimitValue(value, this.IsAngular()) ? this.GetDisplayValue(value).ToString(this.GetFormattingString()) : RJRobotJogResources.INFINITE_LIMIT;
  }

  private void SetValueSensitivity()
  {
    if (this.IsLinear() && !TxApplication.Options.Motion.LimitsCheck)
      this.SliderControl.ValueSensitivityMultiplier = 1000.0;
    else
      this.SliderControl.ValueSensitivityMultiplier = 1.0;
  }

  public void EnterControlsInitMode()
  {
    this.SliderControl.EnterInitMode();
    this.ValueControl.EnterInitMode();
  }

  public void ExitControlsInitMode()
  {
    this.SliderControl.ExitInitMode();
    this.ValueControl.ExitInitMode();
  }

  private void device_JointLowerSoftLimitSet(object sender, TxJoint_LowerSoftLimitSetEventArgs args)
  {
    this.EnterControlsInitMode();
    this.RefreshControls();
    if (TxApplication.Options.Motion.LimitsCheck)
      this.TrimJointValueInLimits();
    this.ExitControlsInitMode();
  }

  private void device_JointUpperSoftLimitSet(object sender, TxJoint_UpperSoftLimitSetEventArgs args)
  {
    this.EnterControlsInitMode();
    this.RefreshControls();
    if (TxApplication.Options.Motion.LimitsCheck)
      this.TrimJointValueInLimits();
    this.ExitControlsInitMode();
  }

  private void SliderControl_ValueChanged(object sender, TxWheel_ValueChangedEventArgs e)
  {
    if (this.IsRefreshingControls())
      return;
    this.m_ownerDeviceData.SetJointValue(this.Joint, this.m_sliderControl.Value);
    this.m_jointJogApplication.RefreshAllControls();
    TxApplication.RefreshDisplay();
  }

  private void ValueControl_ValueChanged(object sender, RoutedEventArgs e)
  {
    if (this.IsRefreshingControls())
      return;
    double num = this.m_valueControl.Value;
    if (CApRJRobotJogJointsLogic.AreEqualDisplayJointValues(num, this.Joint.CurrentValue, this.IsLinear()))
      return;
    this.m_ownerDeviceData.SetJointValue(this.Joint, num);
    this.m_jointJogApplication.RefreshAllControls();
    TxApplication.RefreshDisplay();
  }

  private void Motion_IndicateJointWorkingLimitsCheckChanged(
    object sender,
    TxMotionOptions_IndicateJointWorkingLimitsCheckChangedEventArgs args)
  {
    this.InitControls();
  }

  private void Color_KinematicsColorChanged(
    object sender,
    TxColorOptions_KinematicsColorChangedEventArgs args)
  {
    this.InitControls();
  }

  private void Motion_JointWorkingLimitsChanged(
    object sender,
    TxMotionOptions_JointWorkingLimitsChangedEventArgs args)
  {
    this.InitControls();
  }

  private void CApRJRobotJogJointsApp_OptionsChange(
    object sender,
    CApRJRobotJogJointsLogic.CApRJRobotJogOptionsChangeEventArgs args)
  {
    if (this.IsLinear())
      this.ValueControl.StepSize = args.PrismaticStepSize / TxApplication.Options.Units.LinearMultiplier;
    else if (this.IsAngular())
      this.ValueControl.StepSize = args.RevoluteStepSize / TxApplication.Options.Units.AngularMultiplier;
    double? sliderSensitivity = args.CustomSliderSensitivity;
    if (sliderSensitivity.HasValue)
    {
      this.SliderControl.AutomaticSensitivity = false;
      TxKinematicJointSlider sliderControl = this.SliderControl;
      sliderSensitivity = args.CustomSliderSensitivity;
      double num = sliderSensitivity.Value;
      sliderControl.Sensitivity = num;
    }
    else
      this.SliderControl.AutomaticSensitivity = true;
    this.SetValueSensitivity();
  }

  private void Motion_LimitsCheckChanged(
    object sender,
    TxMotionOptions_LimitsCheckChangedEventArgs args)
  {
    if (TxApplication.Options.Motion.LimitsCheck)
      this.TrimJointValueInLimits();
    this.SetValueSensitivity();
    this.InitControls();
  }

  private void LimitControl_TextChanged(object sender, EventArgs e)
  {
    if (this.IsRefreshingControls())
      return;
    TxKinematicJointLimits textBoxControl = sender as TxKinematicJointLimits;
    if (textBoxControl.IsFocused)
    {
      if (this.IsValidSoftLimit(textBoxControl.Text))
        textBoxControl.SetValueAsValid();
      else
        textBoxControl.SetValueAsInvalid();
    }
    this.SetSoftLomitExceedingHardLimit(textBoxControl);
  }

  private void LimitControl_KeyPress(object sender, KeyEventArgs e)
  {
    if (e.Key != Key.Return)
      return;
    this.UpdateLimitControl(sender);
  }

  private void LimitControl_Enter(object sender, EventArgs e)
  {
    this.m_jointJogApplication.FireLimitsControlsEntered(sender);
  }

  private void LimitControl_LostFocus(object sender, EventArgs e)
  {
    this.UpdateLimitControl(sender);
    this.InitLimitControls();
    this.m_jointJogApplication.FireLimitsControlsLeaved(sender);
  }

  public override void Dispose()
  {
    if (this.m_disposed)
      return;
    this.UnregisterForEvents();
    this.m_disposed = true;
    base.Dispose();
  }
}
